package com.hotchpotch.activity;

import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hotchpotch.R;
import com.hotchpotch.adapter.RecyclerRefreshAdapter;
import com.hotchpotch.base.BaseActivity;
import com.hotchpotch.bean.DBFlowTestBean;
import com.hotchpotch.inter.OnRecyclerViewItemClickListener;
import com.hotchpotch.util.Logg;
import com.raizlabs.android.dbflow.config.FlowManager;
import com.raizlabs.android.dbflow.sql.language.SQLite;
import com.raizlabs.android.dbflow.structure.ModelAdapter;

import java.util.List;

/**
 * Created by Administrator on 2017/7/18 0018.
 * RecyclerView下拉刷新/上拉加载更多
 */

public class RecyclerViewRefreshActivity extends BaseActivity {
    private static final String TAG = "RecyclerViewRefreshActivity";
    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;
    private RecyclerRefreshAdapter adapter;
    private ModelAdapter<DBFlowTestBean> modelAdapter = FlowManager.getModelAdapter(DBFlowTestBean.class);
    private List<DBFlowTestBean> list = null;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recycler_refresh;
    }

    @Override
    protected void initView() {
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipeRefreshLayout.setColorSchemeResources(R.color.color23c751, R.color.colordf262c, R.color.orange);
        recyclerView = (RecyclerView) findViewById(R.id.recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new RecyclerRefreshAdapter(R.layout.item_dbflow);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);//打开动画效果
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initEvents() {
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadMore();
                    }
                }, 1800);
            }
        });
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        loadMore();
                    }
                },1800);
            }
        }, recyclerView);
    }

    @Override
    protected void initData() {
        loadMore();
    }

    private void loadMore() {
        if (list == null || list.size() == 0) {
            list = SQLite.select().from(DBFlowTestBean.class).queryList();
            adapter.setNewData(list);
        } else {
            list = SQLite.select().from(DBFlowTestBean.class).queryList();
            adapter.addData(list);
        }
        if (adapter.getData().size() <= 10) {
            Logg.e(TAG, "加载更多");
            adapter.loadMoreComplete();
        } else {
            Logg.e(TAG, "加载完成");
            adapter.loadMoreEnd();
        }
        swipeRefreshLayout.setRefreshing(false);
        /*if (adapter.getData().size() >= 20) {
            Logg.e(TAG, "隐藏并取消加载更多");
            adapter.disableLoadMoreIfNotFullPage(recyclerView);
        }*/
    }
}
